<?php
/**
 * 用户相关
 *
 * @package ews
 * @ews Enterprise WorkShop
 * @version 1.0 alpha
 * @since 2011-05-03
 * @author monkee
 */

class settingApp extends app
{
	function __construct(){
		$this->_init();
		$this->login_need();
	}
	
	public function index(){
		go_to('/setting/avatar');
	}

	public function avatar(){
		$file = & $this->getData('file/file');
		if(empty($file)){
			return array();
		}		
		$file['size'] = intval($file['size']);
		if($file['size'] === 0){
			$this->inner_msg('请选择文件');
			return array();
		}
		if($file['error'] > 0){
			$this->inner_msg('文件上传出错');
			return array();
		}
		//jpg,png,gif
		$file['name'] = strtolower($file['name']);
		if(!in_array(substr($file['name'], -3),array('png','gif','jpg'))){

			$this->inner_msg('文件类型不符合要求'.substr($file['name'], -3));
			return array();
		}			
		$imagemod = $this->getModel('image');
		$imagemod->newimage($file['tmp_name']);
			
		$imgurl = ROOT.get_avatar($this->user_id, true);
			
		$bool = $imagemod->changeSize(55, $imgurl);
			
		if($bool){
			$this->inner_msg('上传成功！');
			return array('update_avatar' => true);
		}
		else{
			$this->inner_msg('上传失败');
			return array();
		}
	}


	/**
	 * 修改用户的名字
	 */
	public function name()
	{
		$usermod = $this->getModel('user');

		$data = $this->getData('post');
		if(empty($data)){
			//获得数据，是否添加过
			$result = $usermod->username_is_seted($this->user['user_id']);
			return array('isset'=> false, 'uname'=> $this->user['username']);
		}		
		if(empty($data['username'])){
			$this->inner_msg('请填写名字');
			return array();
		}else{
			$result = $usermod->set_username($this->user['user_id'], $data['username']);
			if($result == 1){
				$this->inner_msg('修改成功');
				return array('isset'=> $result, 'uname'=> $data['username']);
			}else{
				$this->inner_msg('修改失败');
				return array();
			}
		}
			
		return array();
	}

	public function mail(){

		$mailmod = $this->getModel('mailer');
		
		$m = $mailmod->get_mailers_by_user_id($this->user['user_id']);
		
		
		$mailcount = $mailmod->get_mailer_count_by_user_id($this->user['user_id']);

		$data = $this->getData('post');	
		if(empty($data))
		{
			return array('m'=> $m);			
		}
		
		
		
		if(empty($data['is_ssl'])){
			$data['is_ssl'] = 0;
		}		
		$mailtype = 'pop3/imap';//mailtype is not get from form.just get the popserver type.
		if($data['email'] == ''){
			$this->inner_msg('请填写邮箱地址');
			return array();
		}
		if(!preg_match('/^[a-z]([a-z0-9]*[-_\.]?[a-z0-9]+)*@([a-z0-9]*[-_]?[a-z0-9]+)+[\.][a-z]{2,3}([\.][a-z]{2})?$/i', $data['email'])){
			$this->inner_msg('邮箱格式有误');
			return array();
		}
		if($data['login_name'] == ''){
			$this->inner_msg('请填写用户名');
			return array();
		}
		if($data['login_password'] == ''){
			$this->inner_msg('请填写用户密码');
			return array();
		}
		if($data['pop_server'] == ''){
			$this->inner_msg('请填写发送服务器');
			return array();
		}
		if($data['pop_port'] == ''){
			$this->inner_msg('请填写发送服务器端口');
			return array();
		}
		if($data['smtp_server'] == ''){
			$this->inner_msg('请填写接收服务器');
			return array();
		}
		if($data['smtp_port'] == ''){
			$this->inner_msg('请填写接收服务器端口');
			return array();
		}

		//mailtype
		if(substr($data['pop_server'], 0, 3) == 'pop'){
			$mailtype = 'pop3';
		}else if(substr($data['pop_server'], 0, 4) == 'ima'){
			$mailtype = 'imap';
		}else{
			//如果什么都不是，那就强制改成pop3
			$mailtype = 'pop3';
		}

		$data['smtp_port'] = intval($data['smtp_port']);
		$data['pop_port'] = intval($data['pop_port']);
		$data['is_ssl'] = intval($data['is_ssl']);
		
		
		
		if($mailcount > 0){
			$result = $mailmod->modify($m[0]['mailer_id'],$this->user['user_id'],$data['email'],$data['login_name'],$data['login_password'],$data['mailer_type'],$data['pop_type'],$data['pop_server'],$data['pop_port'],
			$data['smtp_server'],$data['smtp_port'],$data['is_ssl']);
			if($result == 1){
				$this->inner_msg('修改成功');
				
				$m = $mailmod->get_mailers_by_user_id($this->user['user_id']);
				
				return array('m'=>$m);
				
				
			}else{
				$this->inner_msg('修改失败');
				return array('m'=>$m);
			}
			return array();
		}
		//add		
		else{
			$result = $mailmod->add($this->user['user_id'],$data['email'],$data['login_name'],$data['login_password'],$data['mailer_type'],$data['pop_type'],$data['pop_server'],$data['pop_port'],
			$data['smtp_server'],$data['smtp_port'],$data['is_ssl']);
			if($result == 1){
				$this->inner_msg('添加成功');
				return array();
			}else{
				$this->inner_msg('添加失败');
				return array();
			}
			return array();
		}







	}

	public function theme(){

		$thememod = $this->getModel('theme');
		$usermod = $this->getModel('user');
		$data = $this->getData('post');
		
		$the = $thememod->get_themes();
		
		if(empty($data)){			
			if(!empty($the)){
				return array('th'=>$the);
			}else{
				return array('th' => array());
			}
		}		
		if($data['theme']){
			$uid = intval($this->user['user_id']);			
			$this->user['theme'] = $data['theme'];
			$reuslt = $usermod->set_theme($uid, $data['theme']);
			if($reuslt == 1){
				$this->inner_msg('修改成功');
				//如果使用go_to 就不会显示修改成功了。
				//如果只返回一个数组，那么修改后的主题要刷新后才能看到。
				return array('th'=>$the, 'user' => $this->user);				
			}else{
				$this->inner_msg('修改失败');
				return array('th'=>$the);
			}
		}else{
			$this->inner_msg('请选择主题');
			return array('th'=>$the);
		}
		return array();
	}


	public function password(){
		$data = $this->getData('post');

		if(empty($data)){
			return array();
		}


			
		if($data['newpassword']=='' || $data['confirmpassword']=='' || $data['oldpassword']==''){

			$this->inner_msg('请输入密码');
			return array();
		}
		//two password is equal
		if($data['newpassword'] != $data['confirmpassword']){
			$this->inner_msg('密码不一致');
			return array();
		}

		if($data['oldpassword']== ''){
			$this->inner_msg('请输入原始密码');
			return array();
		}		
		$usermod = & $this->getModel('user');
		$user = $usermod->get_user_by_user_id($this->user['user_id']);
		if($user){
			//old is right?
			$old = md5($data['oldpassword']);
			
			if($user['password'] != $old){
				$this->inner_msg('原始密码不正确');
				return array();
			}
			//change
			$pass = md5($data['newpassword']);//put new password md5
			$result = $usermod->set_password($this->user['user_id'],$pass);
			if($result){
				$result = intval($result);
				if($result == 1){
					$this->inner_msg('修改成功');
					return array();
				}
				else{
					$this->inner_msg('修改失败');
					return array();
				}

			}
		}
	}
	
	public function group(){
		$group = & $this->getModel('group');
		return array('groups' => $group->get_groups_by_user_id($this->user_id));
	}
	
	public function groupadd(){
		$post = & $this->getData('post');
		if(empty($post)){
			$user = & $this->getModel('user');
			return array('users' => $user->get_all_users());
		}
		$flag = empty($post['flag'])?1:$post['flag'];
		$group = & $this->getModel('group');
		$group_id = $group->create($post['groupname'], $this->user_id,$flag, $post['groupmembers']);
		go_to('/setting/group', '创建工作组');
	}
	
	public function groupedit(){
		$id = & $this->getData('get/id');
		$id = intval($id);
		$post = & $this->getData('post');
		$group = & $this->getModel('group');
		if(empty($post)){
			$groups = $group->get_group_by_id($id);
			$user = & $this->getModel('user');
			return array('users' => $user->get_all_users(), 'group' => $groups);
		}
		$group_id = $group->edit($id, $post['groupname'], $post['groupmembers']);
		go_to('/setting/group', '更新工作组');
	}
}
?>
